package com.alm.token.ui.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.bean.AddDrawData;
import com.alm.token.ui.data.bean.DrawIndexData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.TransferTransData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.TurmOutView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.EthWalletUtil;
import com.alm.token.utils.LogUtil;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.yzq.zxinglibrary.android.CaptureActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import baibeiwallet.event.getUserWalletEvent;
import baibeiwallet.utils.DialogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_CODE_VAULE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_INTO_CODE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_NAME_VAULE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_SUCC_CODE;
import static com.alm.token.ui.persenter.MyPreenter.draw;

//import com.yzq.zxinglibrary.android.CaptureActivity;
//import com.uuzuche.lib_zxing.activity.CaptureActivity;
//import com.uuzuche.lib_zxing.activity.CodeUtils;
//import com.uuzuche.lib_zxing.activity.ZXingLibrary;

public class TurnOutActivity extends BaseActivity implements TextWatcher, TurmOutView<Object> {
    @BindView(R.id.rightItem)
    TextView rightItem;
    @BindView(R.id.img_bizhongicon)
    ImageView imgBizhongicon;
    @BindView(R.id.tv_bizhongtv)
    TextView tvBizhongtv;
    @BindView(R.id.tv_totalmoney)
    TextView tvTotalmoney;
    @BindView(R.id.img_selectbizhong)
    ImageView imgSelectbizhong;
    @BindView(R.id.ly_ooutmoneybizhongtop)
    RelativeLayout lyOoutmoneybizhongtop;
    @BindView(R.id.edt_inputadress)
    EditText edtInputadress;

    @BindView(R.id.rly_tagbody)
    RelativeLayout rlyTagbody;
    @BindView(R.id.tv_tagmsgshow)
    TextView tvTagmsgshow;
    @BindView(R.id.edt_inputtag)
    EditText edtInputtag;

    @BindView(R.id.img_saoma)
    ImageView imgSaoma;
    @BindView(R.id.edt_inputmoney)
    EditText edtInputmoney;
    @BindView(R.id.tv_bizhongshow)
    TextView tvBizhongshow;
    @BindView(R.id.tv_mathjiaoyifei)
    TextView tvMathjiaoyifei;
    @BindView(R.id.tv_outmoneycommit)
    TextView tvOutmoneycommit;
    @BindView(R.id.tv_msg)
    TextView tvMsg;
    @BindView(R.id.tv_edtleastnumber)
    TextView tvEdtleastNumber;
    private DrawIndexData.DataBean drawIndexDataBean;
    List<DrawIndexData.DataBean.WalletsBean> walletsBeans = new ArrayList<>();
    private double number = 0d, pro = 0d;

    private double totalMOney = 0d;
    private int bizhongType1 = 1, bizhongselect = 0;
    private static final int QRCODE_SCANNER_REQUEST = 1100, VERIFY_REQUEST = 1101;
    private View rootView;
    private double leatNumber = 0.0000d;


    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        rootView = LayoutInflater.from(TurnOutActivity.this).inflate(R.layout.activity_turn_out, null);

        return R.layout.activity_turn_out;
    }

    @Override
    protected void initVariables() {
        drawIndexDataBean = MyApp.getDrawIndexDataBean();

        //设置EditText的显示方式为多行文本输入
        edtInputadress.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        //文本显示的位置在EditText的最上方
        edtInputadress.setGravity(Gravity.CENTER_VERTICAL);
        edtInputadress.setText("");
        //改变默认的单行模式
        edtInputadress.setSingleLine(false);
        //水平滚动设置为False
        edtInputadress.setHorizontallyScrolling(false);

        edtInputadress.setText(getIntent().getStringExtra("scanResult") != null ? getIntent().getStringExtra("scanResult") + "" : "");


        if (getIntent().getStringExtra("bizhongtype") != null && !getIntent().getStringExtra("bizhongtype").isEmpty()) {//币种页进入
            imgSelectbizhong.setVisibility(View.GONE);
            lyOoutmoneybizhongtop.setOnClickListener(null);
            bizhongType1 = Integer.parseInt(getIntent().getStringExtra("bizhongtype"));

            if (drawIndexDataBean != null) {
                walletsBeans = drawIndexDataBean.getWallets();
                if (walletsBeans != null && walletsBeans.size() != 0) {
                    for (int i = 0; i < walletsBeans.size(); i++) {
                        if (walletsBeans.get(i).getId() == bizhongType1) {
                            tvBizhongtv.setText(walletsBeans.get(i).getName());
                            tvBizhongshow.setText(walletsBeans.get(i).getName());
                            bizhongselect = i;
                            break;
                        }
                    }


                    GlideApp.with(TurnOutActivity.this).load(walletsBeans.get(bizhongselect).getLogo())
                            .listener(new RequestListener<Drawable>() {
                                @Override
                                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                    return false;
                                }

                                @Override
                                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                                    if (EmptyUtils.isNotEmpty(resource)) {
//                                        CommonUtil.changeSVGColor(TurnOutActivity.this, imgBizhongicon, resource, R.color.black);
//                                        return true;
//                                    }
                                    return false;
                                }
                            }).error(R.drawable.icon_arctic).into(imgBizhongicon);
                    totalMOney = walletsBeans.get(bizhongselect).getMoney();
                    tvTotalmoney.setText(CommonUtil.saveToPoint4WithNo(totalMOney) + " " + tvBizhongtv.getText().toString());
                    mathNumber(number);
                }
//                if (walletsBeans.get(bizhongselect).getName().equalsIgnoreCase("MIO")) {
//                    drawIndexDataBean.setProType(2);
//                }
                tvMathjiaoyifei.setText(drawIndexDataBean.getProType() == 2 ? drawIndexDataBean.getPro() * 100 + "%" : drawIndexDataBean.getProMoney());
//                pro = Double.parseDouble(drawIndexDataBean.getProMoney().split(" ")[0]);
                tvMsg.setText(drawIndexDataBean.getMsg().isEmpty() ? "" : drawIndexDataBean.getMsg());
            }

        } else {
            if (drawIndexDataBean != null) {
                walletsBeans = drawIndexDataBean.getWallets();
                if (walletsBeans != null && walletsBeans.size() != 0) {
                    lyOoutmoneybizhongtop.setOnClickListener(this);
                    tvBizhongtv.setText(walletsBeans.get(bizhongselect).getName());
                    tvBizhongshow.setText(walletsBeans.get(bizhongselect).getName());
//                pro = Double.parseDouble(drawIndexDataBean.getProMoney().split(" ")[0]);
                    GlideApp.with(TurnOutActivity.this).load(walletsBeans.get(bizhongselect).getLogo())
                            .listener(new RequestListener<Drawable>() {
                                @Override
                                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                    return false;
                                }

                                @Override
                                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                                    if (EmptyUtils.isNotEmpty(resource)) {
//                                        CommonUtil.changeSVGColor(TurnOutActivity.this, imgBizhongicon, resource, R.color.black);
//                                        return true;
//                                    }
                                    return false;
                                }
                            }).error(R.drawable.icon_arctic).into(imgBizhongicon);
                    totalMOney = walletsBeans.get(bizhongselect).getMoney();
                    tvTotalmoney.setText(CommonUtil.saveToPoint4WithNo(totalMOney) + " " + tvBizhongtv.getText().toString());
                    mathNumber(number);
                } else {
                    imgSelectbizhong.setVisibility(View.GONE);
                    lyOoutmoneybizhongtop.setOnClickListener(null);
                }
//                if (walletsBeans.get(bizhongselect).getName().equalsIgnoreCase("MIO")) {
//                    drawIndexDataBean.setProType(2);
//                }
                tvMathjiaoyifei.setText(drawIndexDataBean.getProType() == 2 ? drawIndexDataBean.getPro() * 100 + "%" : drawIndexDataBean.getProMoney());
                tvMsg.setText(drawIndexDataBean.getMsg().isEmpty() ? "" : drawIndexDataBean.getMsg());
            }
        }

//        if (!TextUtils.isEmpty(edtInputadress.getText().toString()) && tvBizhongtv.getText().toString().indexOf("ETH") != -1) {
//
//            LogUtil.e(edtInputadress.getText().toString());
//            if (!EthWalletUtil.isETHValidAddress(edtInputadress.getText().toString())) {
//                edtInputadress.setText("");
//                showToast(getString(R.string.wallet_hint_warn));
//            } else {
//                scanResult = edtInputadress.getText().toString();
//            }
//        }

        if (!TextUtils.isEmpty(edtInputadress.getText().toString())) {


            //ethereum:0x3a6211d8A8d2B6375815E6d00421456554Bd0011?decimal=18&value=0
            LogUtil.e(edtInputadress.getText().toString());
//            if (!EthWalletUtil.isETHValidAddress(edtInputadress.getText().toString())) {
//                edtInputadress.setText("");
//                showToast(getString(R.string.wallet_hint_warn));
//            } else {
            scanResult = edtInputadress.getText().toString();
            if (scanResult.contains("ethereum")) {
                scanResult = scanResult.replace("ethereum:", "").split("\\?")[0];
                edtInputadress.setText(scanResult);
            }
//            }
        }
    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.title_hint_item12))
                .setRightText(getString(R.string.title_hint_item13));
        edtInputmoney.addTextChangedListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserWalletEvent1(getUserWalletEvent userWalletEvent) {

    }

    @Override
    protected void loadData() {


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_ooutmoneybizhongtop:
                showPopupMenu2(lyOoutmoneybizhongtop);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_saoma, R.id.tv_outmoneycommit, R.id.rightItem})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rightItem:
                Intent intent1 = new Intent(this, VipOrderListActivity.class);
                intent1.putExtra("intentType", 2);
                startActivity(intent1);
                break;
            case R.id.ly_ooutmoneybizhongtop:
                showPopupMenu2(lyOoutmoneybizhongtop);
                break;
            case R.id.img_saoma:
//                showToast("扫码");

                //扫码
                com.tbruyelle.rxpermissions2.RxPermissions rxPermissions = new com.tbruyelle.rxpermissions2.RxPermissions(TurnOutActivity.this);
                rxPermissions.request(Manifest.permission.CAMERA)
                        .subscribe(new Observer<Boolean>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                            }

                            @Override
                            public void onNext(Boolean granted) {
                                if (granted) {
                                    if (!CommonUtil.isCameraOpen()) {
//                                        ZXingLibrary.initDisplayOpinion(getApplicationContext());
//                                        Intent intent = new Intent(TurnOutActivity.this, QRCodeScannerActivity.class);
////                                        Intent intent = new Intent(TurnOutActivity.this, CaptureActivity.class);
//                                        startActivityForResult(intent, QRCODE_SCANNER_REQUEST);

                                        Intent intent = new Intent(TurnOutActivity.this, CaptureActivity.class);
                                        intent.putExtra(com.yzq.zxinglibrary.common.Constant.INTENT_ZXING_CONFIG, MyApp.getZxingConfig());
                                        startActivityForResult(intent, QRCODE_SCANNER_REQUEST);
                                    } else {
                                        showToast(getString(R.string.profile_hint_item1));
                                    }
                                } else {
                                    Toast.makeText(TurnOutActivity.this, getString(R.string.profile_hint_item2), Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onComplete() {
                            }
                        });


//                if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//                    TLPermissionUtil.requestCameraPermissionFromActivity(rootView, this);
//                } else {
//                    if (!CommonUtil.isCameraOpen()) {
//                        Intent intent = new Intent(this, QRCodeScannerActivity.class);
//                        startActivityForResult(intent, QRCODE_SCANNER_REQUEST);
//
//                    } else {
//                        showToast("该相机目前正在被另一个应用程序打开。请关闭其他应用程序，然后再试一次。");
//                    }
//                }

                break;
            case R.id.tv_outmoneycommit:
                if (number < leatNumber) {
                    showToast(getString(R.string.wallet_hint_warn1, CommonUtil.saveToPoint4WithNo(leatNumber)));
//                    number = drawIndexDataBean.getMinDollar();
//                    edtInputmoney.setText("" + number);
                    break;
                }
                scanResult = edtInputadress.getText().toString();

                if (TextUtils.isEmpty(scanResult)) {
                    showToast(getString(R.string.currency_hint1_inputadress));
                    break;
                }
                if (tvBizhongtv.getText().toString().indexOf("ETH") != -1 && !EthWalletUtil.isETHValidAddress(scanResult)) {
                    showToast(getString(R.string.wallet_hint_warn));
                    break;
                }

                if (edtInputtag.getHint().toString().equalsIgnoreCase(getString(R.string.currency_hint1_inputtag_12))
                        && EmptyUtils.isEmpty(edtInputtag.getText())) {
                    showToast(getString(R.string.wallet_hint_xrpmsg, tvBizhongtv.getText().toString()));
                    break;
                }

                if (EmptyUtils.isNotEmpty(MyApp.getUserData()) && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())) {
                    boolean bindMnemonic = MyApp.getUserData().getData().isBindMnemonic();
                    boolean bindGoogleCode = MyApp.getUserData().getData().isBindGoogleCode();
                    boolean payPwd = MyApp.getUserData().getData().isPayPwd();
                    boolean bindEmail = MyApp.getUserData().getData().isBindEmail();
                    if (!payPwd || !bindEmail) {
                        DialogUtils.toBind(this, getSupportFragmentManager(), payPwd, bindEmail, true, true);
                        break;
                    }
                }

                UserWalletData.DataBean.SysMsgBean sysMsgBean = new UserWalletData.DataBean.SysMsgBean();
                sysMsgBean.setTitle(getString(R.string.wallet_hint_warn2));
                sysMsgBean.setContent(getString(R.string.wallet_hint_warn3, CommonUtil.saveToPoint4WithNo(number), tvBizhongshow.getText().toString()));
                DialogUtils.showInfor1(this, sysMsgBean, (pwd) -> {
//                        preenter.getAddDrawData(number + "", scanResult, "", bizhongType1 + "");
                    verify();

//                        Intent intent = new Intent(TurnOutActivity.this, VerifyTurnoutActivity.class);
//                        intent.putExtra("number",number);
//                        intent.putExtra("scanResult",scanResult);
//                        intent.putExtra("bizhongType1",bizhongType1+"");
//                        startActivityForResult(intent,VERIFY_REQUEST);
                });

//                    preenter.getAddDrawData(number + "", scanResult, "", bizhongType1 + "");

                break;
        }
    }

    AlertDialog alert;

    private void verify() {
        alert = DialogUtils.showVerify(this, (pwd, code, google) -> {
            preenter.getAddDrawData(number + "", scanResult, edtInputtag.getText().toString() + "", bizhongType1 + "", "", code, pwd, google);
        }, code1 -> {
            if (MyApp.getUserData() != null && MyApp.getUserData().getData() != null && MyApp.getUserData().getData().getUser() != null) {
                String phone = MyApp.getUserData().getData().getUser().getPhone();
//                preenter.getSmsCodeData(phone, MyPreenter.pay, code1);
                preenter.getSmsCodeData(MyApp.getMail(), draw);
            }
        }, flag -> {
            LogUtil.e("-------------to_TurnOutBiTBActivity");
            startActivityForResult(FundArealistActivity.class, INTENT_RESULT_INTO_CODE);
        });
//        Intent intent = new Intent(TurnOutActivity.this, VerifyTurnoutActivity.class);
//        intent.putExtra("number", number);
//        intent.putExtra("scanResult", scanResult);
//        intent.putExtra("bizhongType1", bizhongType1 + "");
//        startActivityForResult(intent,VERIFY_REQUEST);
    }

    String scanResult = "";

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == QRCODE_SCANNER_REQUEST && resultCode == RESULT_OK && data != null) {
            String result = data.getStringExtra(com.yzq.zxinglibrary.common.Constant.CODED_CONTENT);
//            String result =null;
                    Log.i("TAG", "扫描结果:" + result);
            if (!TextUtils.isEmpty(result)) {
//                if ((tvBizhongtv.getText().toString().indexOf("ETH") != -1 && EthWalletUtil.isETHValidAddress(result)) || tvBizhongtv.getText().toString().indexOf("ETH") == -1) {
                //ethereum:0x3a6211d8A8d2B6375815E6d00421456554Bd0011?decimal=18&value=0
                scanResult = result;
                edtInputadress.setText(result.isEmpty() ? "" : result);
                if (scanResult.contains("ethereum")) {
                    scanResult = scanResult.replace("ethereum:", "").split("\\?")[0];
                    edtInputadress.setText(scanResult);
                }
//                } else {
//                    showToast(getString(R.string.wallet_hint_warn));
//                }
            } else {
                Toast.makeText(TurnOutActivity.this, getString(R.string.profile_hint_item3), Toast.LENGTH_SHORT).show();
            }

        } else if (requestCode == VERIFY_REQUEST && data != null && data.getExtras() != null) {//说明是转出验证后的结果回来

        } else if (requestCode == INTENT_RESULT_INTO_CODE && resultCode == INTENT_RESULT_SUCC_CODE && data != null) {
            String name = data.getStringExtra(INTENT_RESULT_NAME_VAULE);
            String code = data.getStringExtra(INTENT_RESULT_CODE_VAULE);
            LogUtil.e("name=" + name + "  code=" + code);

            if (alert != null && alert.isShowing()) {
                if (DialogUtils.getTvClickphonecountry() != null) {
                    DialogUtils.getTvClickphonecountry().setText(name);
                }
                if (DialogUtils.getTvSetphonecode() != null) {
                    DialogUtils.getTvSetphonecode().setText(code);
                }
            }
//            tvSetphonecode.setText(code);
//            tvClickphonecountry.setText(name);
        } else {
//            Toast.makeText(TurnOutActivity.this, getString(R.string.profile_hint_item4), Toast.LENGTH_SHORT).show();
//                AppUI.success(getContext(), "解析二维码失败");
        }


//        if (requestCode == QRCODE_SCANNER_REQUEST) {
//            if (data != null) {
//                String scanResult1 = data.getStringExtra("scan_result") + "";
//                // 对扫描结果进行处理
////                showToast(scanResult);
//                if(EthWalletUtil.isETHValidAddress(scanResult1)){
//                    scanResult=scanResult1;
//                    edtInputadress.setText(scanResult1.isEmpty() ? "" : scanResult1);
//                }else {
//                    showToast("地址不正确");
//                }
////                ToastUtils.showLongToast(scanResult);
//            }
//        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (s.toString().contains(".")) {
            if (s.length() - 1 - s.toString().indexOf(".") > 4) {
                s = s.toString().subSequence(0, s.toString().indexOf(".") + 5);
                edtInputmoney.setText(s);
                edtInputmoney.setSelection(s.length());
            }
        }
        if (s.toString().trim().substring(0).equals(".")) {
            s = "0" + s;
            edtInputmoney.setText(s);
            edtInputmoney.setSelection(2);
        }

        if (s.toString().startsWith("0")
                && s.toString().trim().length() > 1) {
            if (!s.toString().substring(1, 2).equals(".")) {
                edtInputmoney.setText(s.subSequence(0, 1));
                edtInputmoney.setSelection(1);
                return;
            }
        }

        number = Double.parseDouble(s != null ? (!s.toString().isEmpty() ? s.toString() : "0") : "0");

        if (drawIndexDataBean.getProType() == 2) {

            tvMathjiaoyifei.setText(number == 0 ? drawIndexDataBean.getPro() * 100 + "%" :
                    CommonUtil.saveToPoint4WithNo(drawIndexDataBean.getPro() * number) + drawIndexDataBean.getProMoneyType());
        }
        if (bizhongselect < walletsBeans.size() && number > walletsBeans.get(bizhongselect).getMoney()) {
            showToast(getString(R.string.conversioncurrency_hint1_warn6));
        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    double money = 0d;

    public void mathNumber(double number) {

        if (walletsBeans.get(bizhongselect).getName().equalsIgnoreCase("XRP") || walletsBeans.get(bizhongselect).getName().equalsIgnoreCase("EOS")
        ||walletsBeans.get(bizhongselect).getName().equalsIgnoreCase("HXRP") || walletsBeans.get(bizhongselect).getName().equalsIgnoreCase("HEOS")) {
//            rlyTagbody.setVisibility(View.VISIBLE);
            tvTagmsgshow.setVisibility(View.VISIBLE);
            edtInputtag.setHint(getString(R.string.currency_hint1_inputtag_12));
        } else {
            tvTagmsgshow.setVisibility(View.GONE);
            edtInputtag.setHint(getString(R.string.currency_hint1_inputtag_1));
//            rlyTagbody.setVisibility(View.GONE);
        }

        if (walletsBeans.get(bizhongselect).getPrice() != 0) {
            leatNumber = (drawIndexDataBean.getMinDollar() / walletsBeans.get(bizhongselect).getPrice());
        } else {
            leatNumber = 0;
        }
//        leatNumber = (drawIndexDataBean.getMinDollar() / walletsBeans.get(bizhongselect).getPrice());
        LogUtil.e("leatNumber1=" + leatNumber);
        BigDecimal b = new BigDecimal(leatNumber);
        leatNumber = b.setScale(4, BigDecimal.ROUND_UP).doubleValue();
        LogUtil.e("leatNumber2=" + leatNumber);
        tvEdtleastNumber.setText(getString(R.string.currency_hint1_leastinput, CommonUtil.saveToPoint4WithNo(leatNumber)));
        money = number * walletsBeans.get(bizhongselect).getPrice();
//        tvMathmoney.setText("≈" + money + "USD/" + walletsBeans.get(bizhongType1).getName());
//        tvMathjiaoyifei.setText(pro + walletsBeans.get(bizhongselect).getName()
//                + "≈$" + dec1.format(money * pro));

//        dec1 = new DecimalFormat("0.00");
//        edtMathnumber.setText(dec1.format((money-money * pro)/walletsBeans.get(bizhongType2).getPrice()));
    }

    public void showPopupMenu2(View view) {
        //参数View 是设置当前菜单显示的相对于View组件位置，具体位置系统会处理
        PopupMenu popupMenu = new PopupMenu(this, view);
        //加载menu布局
        Menu menu = popupMenu.getMenu();
        for (DrawIndexData.DataBean.WalletsBean walletsBean : walletsBeans) {
            menu.add(walletsBean.getName());
        }
        popupMenu.getMenuInflater().inflate(R.menu.menu_popmenu, menu);


        //设置menu中的item点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                for (int i = 0; i < walletsBeans.size(); i++) {
                    if (item.getTitle().toString().equalsIgnoreCase(walletsBeans.get(i).getName())) {
                        if (bizhongType1 == walletsBeans.get(i).getId()) {
                            showToast(getString(R.string.crowdfund_hint_warn3));
                            break;
                        }
                        bizhongselect = i;
                        bizhongType1 = walletsBeans.get(i).getId();//0 1 2 3
                        GlideApp.with(TurnOutActivity.this).load(walletsBeans.get(i).getLogo())
                                .listener(new RequestListener<Drawable>() {
                                    @Override
                                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                        return false;
                                    }

                                    @Override
                                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                                        if (EmptyUtils.isNotEmpty(resource)) {
//                                            CommonUtil.changeSVGColor(TurnOutActivity.this, imgBizhongicon, resource, R.color.black);
//                                            return true;
//                                        }
                                        return false;
                                    }
                                }).error(R.drawable.icon_arctic).into(imgBizhongicon);
                        tvBizhongtv.setText(item.getTitle().toString());
                        tvBizhongshow.setText(item.getTitle().toString());
                        totalMOney = walletsBeans.get(i).getMoney();
//                        if (walletsBeans.get(bizhongselect).getName().equalsIgnoreCase("MIO")) {
//                            drawIndexDataBean.setProType(2);
//                        }else {
//                            drawIndexDataBean.setProType(1);
//                        }
                        edtInputmoney.setText("");
                        tvMathjiaoyifei.setText(drawIndexDataBean.getProType() == 2 ? drawIndexDataBean.getPro() * 100 + "%" : drawIndexDataBean.getProMoney());
                        tvTotalmoney.setText(CommonUtil.saveToPoint4WithNo(totalMOney) + " " + tvBizhongtv.getText().toString());
                        mathNumber(number);
                        break;
                    }
                }
                return true;
            }
        });
        popupMenu.show();
    }

    @Override
    public void getData(Object o) {

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this,PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();

    }

    @Override
    public void getDrawIndexData(DrawIndexData drawIndexData) {

    }

    @Override
    public void getAddDrawData(AddDrawData addDrawData) {
        showToast(addDrawData.getMsg());
        if (alert != null && alert.isShowing()) {
            alert.dismiss();
        }
        if (addDrawData.getStatus() == 0) {
            EventBus.getDefault().post(new getUserWalletEvent(null));
            finish();
        }
    }

    @Override
    public void getCode(RegisterData registerData) {
//        if (registerData.getMsg().equalsIgnoreCase(getString(R.string.profile_hint_warn15))) {
//            showToast(getString(R.string.profile_hint_warn14));
//        } else {
//            showToast(registerData.getMsg());
//        }
        showToast(registerData.getMsg());
    }

    @Override
    public void getTransferIndexData(DrawIndexData drawIndexData) {

    }

    @Override
    public void getTransfertransData(TransferTransData transferTransData) {

    }

    @Override
    public void getcheckLoginNameData(RegisterData checkLoginNameData) {

    }

}
